﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GAMS.DAL;
using KPIS.GERP.GAMS.MODEL;
using System.Collections.Generic;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ACM_BOOK_Bank : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        M_ACM_BOOK_Bank m_acm_book_bank = new M_ACM_BOOK_Bank();
        D_ACM_BOOK_Bank d_acm_book_bank = new D_ACM_BOOK_Bank();
        D_ACM_MAS_Account d_acm_mas_account = new D_ACM_MAS_Account();
        //D_ACM_Account_Version d_acm_account_version = new D_ACM_Account_Version();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UpdateUI1("Load"); 
               // LoadFilterToAccountCode();
                btnAccountSearch.OnClientClick = "openWin(" + cmbAccountCode.ClientID + ")";
                grdACMBookBank.DataSource = d_acm_book_bank.GetList();
                grdACMBookBank.DataBind();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (checkValidate() == true)
            {
                UpdateUI1("Save");
                m_acm_book_bank.bank_acc_seq = Convert.ToInt32(hbank_acc_seq.Text);
                m_acm_book_bank.account_seq = main.ConvertNullInt(cmbAccountCode.SelectedValue);//Convert.ToInt32(txtAccountCode.Text);//main.ConvertNullInt(hAccount_seq.Text);
                m_acm_book_bank.updated_by = Convert.ToInt32(main.strEmpID);
                m_acm_book_bank.updated_when = DateTime.Now;
                d_acm_book_bank.Update(m_acm_book_bank);
                grdACMBookBank.DataSource = d_acm_book_bank.GetList();
                grdACMBookBank.DataBind();
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI1("Cancel");
            ClearObj();
        }
        protected void grdACMBookBank_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                grdACMBookBank.DataSource = d_acm_book_bank.GetList();
                grdACMBookBank.DataBind();
            }
        }
        protected void grdACMBookBank_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "DataEdit")
            {
                cmbAccountCode.Focus();
                UpdateUI1("Update");
                M_ACM_BOOK_Bank info = d_acm_book_bank.GetListFillBySeq(main.ConvertNullInt(grdACMBookBank.Items[e.Item.ItemIndex]["bank_acc_seq"].Text));
                txtBookBankNo.Text = info.bank_acc_no;
                txtBookBankName.Text = info.bank_acc_name;
                txtBankName.Text = info.bank_name;
                hbank_acc_seq.Text = info.bank_acc_seq.ToString();

                M_ACM_MAS_Account infoacc = d_acm_mas_account.GetListFillByAccountSeq(info.account_seq);
                cmbAccountCode.Text = infoacc.account_code_name;
            }
            else
            {
                grdACMBookBank.DataSource = d_acm_book_bank.GetList();
            }
            
        }

        //protected void LoadFilterToAccountCode()
        //{
        //    cmbAccountCode.DataSource = d_acm_mas_account.GetListAccount();
        //    cmbAccountCode.DataBind();

        //    main.InsertEmptyDDLSelect(cmbAccountCode);
            
        //}


        protected void cmbAccountCode_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbAccountCode.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }


        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            string strArg;
            strArg = e.Argument;
            cmbAccountCode.SelectedValue = strArg;
            M_ACM_MAS_Account infoacc = d_acm_mas_account.GetListFillByAccountSeq(Convert.ToInt32(strArg));
            cmbAccountCode.Text = infoacc.account_code_name;
        }

        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "Load":
                    lblStatus.Text = "แสดงสถานะการทำงาน";
                    main.StrSelectWord = "< เลือกรายการรหัสบัญชี >";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    btnAccountSearch.Enabled = false;
                    txtAccountCode.Enabled = false;
                    cmbAccountCode.Enabled = false;
                    ClearObj();
                    break;
                case "Update":
                    lblStatus.Text = "แก้ไขข้อมูลจากที่เลือก";
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    btnAccountSearch.Enabled = true;
                    txtAccountCode.Enabled = true;
                    cmbAccountCode.Enabled = true;
                    break;
                case "Save":
                    lblStatus.Text = "บันทึกข้อมูลเรียบร้อยแล้ว";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    btnAccountSearch.Enabled = false;
                    txtAccountCode.Enabled = false;
                    cmbAccountCode.Enabled = false;
                    break;
                case "Cancel":
                    lblStatus.Text = "ยกเลิกข้อมูล";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    btnAccountSearch.Enabled = false;
                    txtAccountCode.Enabled = false;
                    cmbAccountCode.Enabled = false;
                    ClearObj();
                    break;
            }
        }
        private void ClearObj()
        {
            txtBookBankNo.Text = "";
            txtBookBankName.Text = "";
            txtBankName.Text = "";
            cmbAccountCode.SelectedValue = "";
        }
        private bool checkValidate()
        {
            bool pFlag = true;
            if (cmbAccountCode.Text.Trim() == "< เลือกรายการรหัสบัญชี >")
            {
                cmbAccountCode.Focus();
                lblStatus.Text = "กรุณากรอกข้อมูลของรหัสบัญชีก่อนบันทึกรายการด้วย";
                pFlag = false;
            }
            //if (txtAccountCode.Text.Trim() == "")
            //{
            //    lblStatus.Text = "กรุณากรอกข้อมูลของรหัสบัญชีก่อนบันทึกรายการด้วย";
            //    pFlag = false;
            //}
            return pFlag;
        }

    }
}
